﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Text;
using System.Collections.Generic;
using KMF.WebUI.Models;

namespace KMF.WebUI.CustomHelpers
{
    public static class CustomHtmlHelpers
    {
        public static MvcHtmlString CustomStyleDropDownList<T>(this HtmlHelper html,string name,object objectSelected, IEnumerable<T> source,string TextField,string ValueField, string FirstItemText,string FirstItemValue)
        {
            StringBuilder builder = new StringBuilder();
            TagBuilder tag = new TagBuilder("div");
            tag.MergeAttribute("class", "dropdownlist");
            builder.Append(tag.ToString(TagRenderMode.StartTag));
            tag = new TagBuilder("select");
            tag.MergeAttribute("name", name);
            tag.MergeAttribute("id", name);
            builder.Append(tag.ToString(TagRenderMode.StartTag));
            foreach(var o in source)
            {
                tag = new TagBuilder("option");
                tag.MergeAttribute("value", typeof (T).GetProperty(ValueField).GetValue(o, null).ToString());
                if(typeof (T).GetProperty(ValueField).GetValue(o, null).Equals(objectSelected))
                {
                    tag.MergeAttribute("selected", "selected");
                }
                tag.SetInnerText(typeof (T).GetProperty(TextField).GetValue(o, null).ToString());
                builder.Append(tag);
            }
            builder.Append("</select></div><!--End div.dropdownlist-->");

            return MvcHtmlString.Create(builder.ToString());
        }

        public static MvcHtmlString CustomStyleAbbrDropDownList<T>(this HtmlHelper html, string name, object objectSelected, string textSelected, IEnumerable<T> source, string textField, string valueField,string abbrField, string firstItemText, string firstItemValue,string selectIconUrl)
        {
            StringBuilder builder = new StringBuilder();
            TagBuilder tag = new TagBuilder("div");
            tag.MergeAttribute("class", "select-panel");
            builder.Append(tag.ToString(TagRenderMode.StartTag));
            if (objectSelected != null )
            {
                builder.AppendFormat("<input type='text' readonly='readonly' class='select-text' value='{0}'/>", textSelected);
                builder.AppendFormat("<input type='hidden' class='select-value' name='{0}' value='{1}'/>", name,
                                     objectSelected);
            }
            else
            {
                builder.AppendFormat("<input type='text' readonly='readonly' class='select-text' value='{0}'/>", firstItemText);
                builder.AppendFormat("<input type='hidden' class='select-value' name='{0}' value='{1}'/>", name,
                                     firstItemValue);
            }
            builder.AppendFormat("<img src='{0}' class='select-button' />", selectIconUrl);
            builder.Append("<div class='dropdown-panel'>");
            builder.AppendFormat(
                "<p class='select-item'><input type='hidden' value='{0}'/><span class='abbr'>{1}</span><span class='text'>{2}</span></p>",
                firstItemValue, "", firstItemText);
            foreach (var o in source)
            {    
                builder.AppendFormat(
                    "<p class='select-item'><input type='hidden' value='{0}'/><span class='abbr'>{1}</span><span class='text'>{2}</span></p>",
                    typeof (T).GetProperty(valueField).GetValue(o, null),
                    typeof (T).GetProperty(abbrField).GetValue(o, null),
                    typeof (T).GetProperty(textField).GetValue(o, null));
            }
            builder.Append("</div><!--End div.dropdown-panel--></div><!--End div.select-panel-->");

            return MvcHtmlString.Create(builder.ToString());
        }

        public static MvcHtmlString CustomStyleNormalDropDownList<T>(this HtmlHelper html, string name, object objectSelected, IEnumerable<T> source, string textField, string valueField, string firstItemText, string firstItemValue, string selectIconUrl)
        {
            StringBuilder builder = new StringBuilder();
            TagBuilder tag = new TagBuilder("div");
            tag.MergeAttribute("class", "select-panel");
            builder.Append(tag.ToString(TagRenderMode.StartTag));
            if (objectSelected != null)
            {
                string textSelected = "";
                foreach (T item in source)
                {
                    if(typeof(T).GetProperty(valueField).GetValue(item,null).Equals(objectSelected))
                    {
                        textSelected = typeof (T).GetProperty(textField).GetValue(item, null).ToString();
                        break;
                    }
                }
                builder.AppendFormat("<input type='text' readonly='readonly' class='select-text' value='{0}'/>", textSelected);
                builder.AppendFormat("<input type='hidden' class='select-value' name='{0}' id='{0}' value='{1}'/>", name,
                                     objectSelected);
            }
            else
            {
                builder.AppendFormat("<input type='text' readonly='readonly' class='select-text' value='{0}'/>", firstItemText);
                builder.AppendFormat("<input type='hidden' class='select-value' name='{0}' id='{0}' value='{1}'/>", name,
                                     firstItemValue);
            }
            builder.AppendFormat("<img src='{0}' class='select-button' />", selectIconUrl);
            builder.Append("<div class='dropdown-panel'>");
            if (!string.IsNullOrEmpty(firstItemText))
            {
                builder.AppendFormat(
                    "<p class='select-item'><input type='hidden' value='{0}'/><span class='abbr'>{1}</span><span class='text'>{2}</span></p>",
                    firstItemValue, "", firstItemText);
            }
            foreach (var o in source)
            {
                builder.AppendFormat(
                    "<p class='select-item'><input type='hidden' value='{0}'/><span class='text'>{1}</span></p>",
                    typeof(T).GetProperty(valueField).GetValue(o, null),
                    typeof(T).GetProperty(textField).GetValue(o, null));
            }
            builder.Append("</div><!--End div.dropdown-panel--></div><!--End div.select-panel-->");

            return MvcHtmlString.Create(builder.ToString());
        }

        public static MvcHtmlString DropDownListWithOptGroup<T>(this HtmlHelper html, string name, object objectSelected, IEnumerable<T> source, string textField, string valueField, string firstItemText, string firstItemValue, string selectIconUrl,string parentIdField)
        {
            StringBuilder builder = new StringBuilder();
            TagBuilder tag = new TagBuilder("div");
            tag.MergeAttribute("class", "select-panel");
            builder.Append(tag.ToString(TagRenderMode.StartTag));
            if (objectSelected != null)
            {
                string textSelected = "";
                foreach (T item in source)
                {
                    if (typeof(T).GetProperty(valueField).GetValue(item, null).Equals(objectSelected))
                    {
                        textSelected = typeof(T).GetProperty(textField).GetValue(item, null).ToString();
                        break;
                    }
                }
                builder.AppendFormat("<input type='text' readonly='readonly' class='select-text' value='{0}'/>", textSelected);
                builder.AppendFormat("<input type='hidden' class='select-value' name='{0}' id='{0}' value='{1}'/>", name,
                                     objectSelected);
            }
            else
            {
                builder.AppendFormat("<input type='text' readonly='readonly' class='select-text' value='{0}'/>", firstItemText);
                builder.AppendFormat("<input type='hidden' class='select-value' name='{0}' id='{0}' value='{1}'/>", name,
                                     firstItemValue);
            }
            builder.AppendFormat("<img src='{0}' class='select-button' />", selectIconUrl);
            builder.Append("<div class='dropdown-panel'>");
            if (!string.IsNullOrEmpty(firstItemText))
            {
                builder.AppendFormat(
                    "<p class='select-item'><input type='hidden' value='{0}'/><span class='abbr'>{1}</span><span class='text'>{2}</span></p>",
                    firstItemValue, "", firstItemText);
            }

            var listParent = new List<T>();
            foreach (var p in source)
            {
                if (typeof(T).GetProperty(parentIdField).GetValue(p, null).Equals(0))
                {
                    listParent.Add(p);
                }
            }

            foreach (var parent in listParent)
            {
                builder.AppendFormat(
                            "<p class='optgroup-item'>{0}</p>",
                            typeof(T).GetProperty(textField).GetValue(parent, null));
                var parentId = (int) typeof (T).GetProperty(valueField).GetValue(parent, null);
                foreach (var o in source)
                {
                    if (typeof(T).GetProperty(parentIdField).GetValue(o, null).Equals(parentId))
                    {
                        builder.AppendFormat(
                            "<p class='select-item'><input type='hidden' value='{0}'/><span class='text'>{1}</span></p>",
                            typeof (T).GetProperty(valueField).GetValue(o, null),
                            typeof (T).GetProperty(textField).GetValue(o, null));
                    }
                }
            }
            builder.Append("</div><!--End div.dropdown-panel--></div><!--End div.select-panel-->");

            return MvcHtmlString.Create(builder.ToString());
        }

        public static MvcHtmlString DropDownWithImage<T>(this HtmlHelper html,string name, string id,object objectSelected,IEnumerable<T> source,string textField, string valueField, string imageField,string rootImageUrl,string iconSelectUrl,string defaultValue,string defaultImage,string defaultText)
        {
            StringBuilder select = new StringBuilder("<div class='lang-select'>");
            StringBuilder langItem = new StringBuilder();
            string currentLang = string.Empty;

            select.Append(string.Format("<img src='{0}' class='lang-dropicon'/>",iconSelectUrl));

            foreach (T lang in source ?? new List<T>())
            {
                var value = typeof (T).GetProperty(valueField).GetValue(lang, null) ?? "";
                var text = typeof (T).GetProperty(textField).GetValue(lang, null) ?? "";
                var image = typeof (T).GetProperty(imageField).GetValue(lang, null) ?? "";
                //Set current item
                if (string.Compare(objectSelected.ToString().Trim(),value.ToString().Trim(),true) == 0)
                {
                    currentLang =
                        string.Format("<p class='current-lang'><input type='hidden' class='lang-code' value='{0}'/><img src='{1}'/><span>{2}</span></p>", value, string.Format("{0}/{1}", rootImageUrl, image), text);
                    select.Append(string.Format("<input type='hidden' class='lang-code-value' name='{0}' id='{1}' value='{2}'/>", name, id, value));
                }
                //Get list item
                langItem.Append(
                    string.Format(
                        "<p class='lang-item'><input type='hidden' class='lang-code' value='{0}'/><img src='{1}'/><span>{2}</span></p>",
                        value, string.Format("{0}/{1}", rootImageUrl, image), text));
            }
            if(string.IsNullOrEmpty(currentLang))
            {
                currentLang =
                        string.Format("<p class='current-lang'><input type='hidden' class='lang-code' value='{0}'/><img src='{1}'/><span>{2}</span></p>", defaultValue, string.Format("{0}/{1}", rootImageUrl, defaultImage), defaultText);
                select.Append(string.Format("<input type='hidden' class='lang-code-value' name='{0}' id='{1}' value='{2}'/>", name, id, defaultValue));
            }

            select.Append(currentLang);
            select.Append("<div class='lang-drop-panel hidden'>");
            select.Append(langItem);
            select.Append("</div></div>");

            return MvcHtmlString.Create(select.ToString());
        }

        public static MvcHtmlString MonthDropDownList(this HtmlHelper html,string name, object objectSelected,string defaultFirstValue)
        {
            StringBuilder result = new StringBuilder();
            result.Append(string.Format("<select name='{0}' id='{0}'>", name));
            TagBuilder oTag = new TagBuilder("option");
            oTag.MergeAttribute("value", defaultFirstValue);
            oTag.SetInnerText("Month");
            result.Append(oTag.ToString());
            for (int i = 1; i <= 12; i++)
            {
                oTag = new TagBuilder("option");
                oTag.MergeAttribute("value", i.ToString().PadLeft(2,'0'));
                oTag.SetInnerText((new DateTime(DateTime.MinValue.Year, i, 1)).ToString("MMMM"));
                if(objectSelected.ToString().PadLeft(2,'0') == i.ToString().PadLeft(2,'0'))
                {
                    oTag.MergeAttribute("selected", "selected");
                }
                result.Append(oTag.ToString());

            }
            result.Append("</select>");
            return MvcHtmlString.Create(result.ToString());
        }

        public static MvcHtmlString YearDropDownList(this HtmlHelper html, string name, object objectSelected, string defaultFirstValue,int minYear, int maxYear)
        {
            var result = new StringBuilder();
            result.Append(string.Format("<select name='{0}' id='{0}'>", name));
            var oTag = new TagBuilder("option");
            oTag.MergeAttribute("value", defaultFirstValue);
            oTag.SetInnerText("Year");
            result.Append(oTag.ToString());
            for (int i = minYear; i <= maxYear; i++)
            {
                oTag = new TagBuilder("option");
                oTag.MergeAttribute("value", i.ToString());
                oTag.SetInnerText(i.ToString());
                if (objectSelected.ToString() == i.ToString())
                {
                    oTag.MergeAttribute("selected", "selected");
                }
                result.Append(oTag);

            }
            result.Append("</select>");
            return MvcHtmlString.Create(result.ToString());
        }

        public static MvcHtmlString NumberDropDownList(this HtmlHelper html, string name, object objectSelected, bool hasFirstItem, string defaultFirstText, string defaultFirstValue, bool hasLastItem, string DefaultLastText,string DefaultLastValue, int MinValue, int MaxValue, string prefix, string suffix)
        {
            StringBuilder result = new StringBuilder();
            result.Append(string.Format("<select name='{0}' id='{0}'>", name));
            TagBuilder oTag;
            if (hasFirstItem)
            {
                oTag = new TagBuilder("option");
                oTag.MergeAttribute("value", defaultFirstValue);
                oTag.SetInnerText(defaultFirstText);
                result.Append(oTag.ToString());
            }
            for (int i = MinValue; i <= MaxValue; i++)
            {
                oTag = new TagBuilder("option");
                oTag.MergeAttribute("value", i.ToString());
                oTag.SetInnerText(string.Format("{0}{1}{2}", prefix, i, suffix));
                if (objectSelected.ToString() == i.ToString())
                {
                    oTag.MergeAttribute("selected", "selected");
                }
                result.Append(oTag);

            }
            if (hasLastItem)
            {
                oTag = new TagBuilder("option");
                oTag.MergeAttribute("value", DefaultLastValue);
                oTag.SetInnerText(DefaultLastText);
                result.Append(oTag.ToString());
            }
            result.Append("</select>");
            return MvcHtmlString.Create(result.ToString());
        }

        public static MvcHtmlString StatusDropDownList(this HtmlHelper html, string selected,string name)
        {
            var builder = new StringBuilder();
            var tag = new TagBuilder("select");
            tag.MergeAttribute("class", "dropdownlist");
            tag.MergeAttribute("name", name);
            tag.MergeAttribute("id", name);
            builder.Append(tag.ToString(TagRenderMode.StartTag));

            // #1
            for (int i = 0; i < 3; i++)
            {
                tag = new TagBuilder("option");
                if(i.ToString()==selected)
                {
                    tag.MergeAttribute("selected", "selected");
                }
                tag.MergeAttribute("value", i.ToString());
                var txt = string.Empty;
                switch (i)
                {
                    case 0:
                        txt = "Toàn bộ";
                        break;
                    case 1:
                        txt = "Kích hoạt";
                        break;
                    case 2:
                        txt = "Không kích hoạt";
                        break;
                }
                tag.SetInnerText(txt);
                builder.Append(tag);
            }
            builder.Append("</select>");

            return MvcHtmlString.Create(builder.ToString());
        }


        public static MvcHtmlString RadioPreSelected(this HtmlHelper html,string name,object value,object currentValue,string htmlClass)
        {
            TagBuilder tag = new TagBuilder("input");
            tag.MergeAttribute("type", "radio");
            tag.MergeAttribute("name", name);
            tag.MergeAttribute("class",htmlClass);
            tag.MergeAttribute("value", (value ?? "_radio_noname").ToString());
            if(currentValue != null && currentValue.Equals(value))
            {
                tag.MergeAttribute("checked", "checked");
                //tag.MergeAttribute("test","test");
            }
            return MvcHtmlString.Create(tag.ToString(TagRenderMode.SelfClosing));
        }

        public static MvcHtmlString TimeDropDownList(this HtmlHelper html,string name,string selectedValue,bool is24H)
        {
            StringBuilder result = new StringBuilder();
            TagBuilder tag = new TagBuilder("select");
            tag.MergeAttribute("name", name);
            tag.MergeAttribute("id", name);
            result.Append(tag.ToString(TagRenderMode.StartTag));
            for(int i=0; i<=23;i++)
            {
                tag = new TagBuilder("option");
                var hour = is24H ? i : (i != 12 ? i%12 : i);
                var am_pm = is24H ? "" : (i < 12 ? "AM" : "PM");
                var value = string.Format("{0}:00 {1}", hour.ToString().PadLeft(2,'0'), am_pm);
                tag.MergeAttribute("value", value);
                if (value == selectedValue)
                    tag.MergeAttribute("selected", "selected");
                tag.SetInnerText(value);
                result.Append(tag.ToString(TagRenderMode.Normal));
            }
            result.Append("</select>");
            return MvcHtmlString.Create(result.ToString());
        }

        public static MvcHtmlString TimePicker(this HtmlHelper html,string name,string selectedValue, bool readOnly,int height,int width,string urlClockIcon)
        {
            StringBuilder result = new StringBuilder("<div>");
            TagBuilder input = new TagBuilder("input");
            input.MergeAttribute("type", "text");
            input.MergeAttribute("name", name);
            input.MergeAttribute("id", name);
            input.MergeAttribute("onFocus", "focus_time_picker(this)");
            input.MergeAttribute("onBlur", "blur_time_picker(this)");
            input.MergeAttribute("value", selectedValue);
            if (readOnly)
                input.MergeAttribute("readonly","readyonly");
            input.MergeAttribute("style", string.Format("height:{0}px; width: {1}px;", height, width));
            result.Append(input.ToString());

            TagBuilder image = new TagBuilder("img");
            image.MergeAttribute("src", urlClockIcon);
            image.MergeAttribute("parentId", name);
            image.MergeAttribute("onclick", "toggle_time_picker(this)");
            image.MergeAttribute("class","img-choose-time");
            image.MergeAttribute("title", "Choose time");

            result.Append(image.ToString());

            StringBuilder picker_container = new StringBuilder();
            picker_container.AppendFormat(
                "<div class='time-picker' parentId='{0}'><p class='time-title'>Choose Time</p><div class='time-column'><p>12:00 AM</p><p>01:00 AM</p><p>02:00 AM</p><p>03:00 AM</p><p>04:00 AM</p><p>05:00 AM</p><p>06:00 AM</p><p>07:00 AM</p></div><!--End div .timer-column--><div class='time-column'><p>08:00 AM</p><p>09:00 AM</p><p>10:00 AM</p><p>11:00 AM</p><p>12:00 PM</p><p>01:00 PM</p><p>02:00 PM</p><p>03:00 PM</p></div><!--End div .timer-column--><div class='time-column'><p>04:00 PM</p><p>05:00 PM</p><p>06:00 PM</p><p>07:00 PM</p><p>08:00 PM</p><p>09:00 PM</p><p>10:00 PM</p><p>11:00 PM</p></div><!--End div .timer-column--></div>",
                name);
            result.Append(picker_container.ToString());
            result.Append("</div>");

            return MvcHtmlString.Create(result.ToString());

        }

        public static MvcHtmlString PageLinks(this HtmlHelper html, PageInfo pageInfo, Func<int, string> pageUrl)
        {
            if (pageInfo.TotalPages > 0)
            {
                StringBuilder result = new StringBuilder();
                if (pageInfo.TotalPages > 1 && pageInfo.CurrentPage > 1)
                {
                //Tạo link về đầu
                TagBuilder FirstTag = new TagBuilder("a");
                FirstTag.MergeAttribute("href", pageUrl(1));
                FirstTag.MergeAttribute("class", "first_paging");
                FirstTag.InnerHtml = "Đầu";
                result.AppendLine(FirstTag.ToString());
                }
                if (pageInfo.CurrentPage > 1)
                {
                //Tạo link Trước nếu currentPage > 1
                TagBuilder tag = new TagBuilder("a");
                tag.MergeAttribute("href", pageUrl(pageInfo.CurrentPage - 1));
                tag.MergeAttribute("class", "previous_paging");
                tag.InnerHtml = "Sau";
                result.AppendLine(tag.ToString());
                }
                for (int i = 1; i <= pageInfo.TotalPages; i++)
                {
                    TagBuilder tag = new TagBuilder("a");
                    tag.MergeAttribute("href", pageUrl(i));
                    tag.InnerHtml = i.ToString();
                    tag.AddCssClass("number_paging");
                    if (i == pageInfo.CurrentPage)
                        tag.AddCssClass("current_page");
                    result.AppendLine(tag.ToString());
                }
                if (pageInfo.CurrentPage < pageInfo.TotalPages)
                {
                //Tạo link Sau nếu currentPage < TotalPages

                TagBuilder tag = new TagBuilder("a");
                tag.MergeAttribute("href", pageUrl(pageInfo.CurrentPage + 1));
                tag.MergeAttribute("class", "next_paging");
                tag.InnerHtml = "Tiếp";
                result.AppendLine(tag.ToString());
                }

                if (pageInfo.TotalPages > 1 && pageInfo.CurrentPage < pageInfo.TotalPages)
                {
                //Tạo link về cuối
                TagBuilder LastTag = new TagBuilder("a");
                LastTag.MergeAttribute("href", pageUrl(pageInfo.TotalPages));
                LastTag.MergeAttribute("class", "last_paging");
                LastTag.InnerHtml = "Cuối";
                result.AppendLine(LastTag.ToString());
                }
                result.AppendLine(" (" + pageInfo.TotalPages + " Trang)");
                return MvcHtmlString.Create(result.ToString());

            }
            else
                return MvcHtmlString.Create("");
        }

        public static MvcHtmlString PageLinksRelExternal(this HtmlHelper html, PageInfo pageInfo, Func<int, string> pageUrl)
        {
            if (pageInfo.TotalPages > 0)
            {
                StringBuilder result = new StringBuilder();
                if (pageInfo.TotalPages > 1 && pageInfo.CurrentPage > 1)
                {
                    //Tạo link về đầu
                    TagBuilder FirstTag = new TagBuilder("a");
                    FirstTag.MergeAttribute("href", pageUrl(1));
                    FirstTag.MergeAttribute("rel","external");
                    FirstTag.MergeAttribute("class", "first_paging");
                    FirstTag.InnerHtml = "Đầu";
                    result.AppendLine(FirstTag.ToString());
                }
                if (pageInfo.CurrentPage > 1)
                {
                    //Tạo link Trước nếu currentPage > 1
                    TagBuilder tag = new TagBuilder("a");
                    tag.MergeAttribute("href", pageUrl(pageInfo.CurrentPage - 1));
                    tag.MergeAttribute("rel", "external");
                    tag.MergeAttribute("class", "previous_paging");
                    tag.InnerHtml = "Sau";
                    result.AppendLine(tag.ToString());
                }
                for (int i = 1; i <= pageInfo.TotalPages; i++)
                {
                    TagBuilder tag = new TagBuilder("a");
                    tag.MergeAttribute("href", pageUrl(i));
                    tag.MergeAttribute("rel", "external");
                    tag.InnerHtml = i.ToString();
                    tag.AddCssClass("number_paging");
                    if (i == pageInfo.CurrentPage)
                        tag.AddCssClass("current_page");
                    result.AppendLine(tag.ToString());
                }
                if (pageInfo.CurrentPage < pageInfo.TotalPages)
                {
                    //Tạo link Sau nếu currentPage < TotalPages

                    TagBuilder tag = new TagBuilder("a");
                    tag.MergeAttribute("href", pageUrl(pageInfo.CurrentPage + 1));
                    tag.MergeAttribute("rel", "external");
                    tag.MergeAttribute("class", "next_paging");
                    tag.InnerHtml = "Tiếp";
                    result.AppendLine(tag.ToString());
                }

                if (pageInfo.TotalPages > 1 && pageInfo.CurrentPage < pageInfo.TotalPages)
                {
                    //Tạo link về cuối
                    TagBuilder LastTag = new TagBuilder("a");
                    LastTag.MergeAttribute("href", pageUrl(pageInfo.TotalPages));
                    LastTag.MergeAttribute("rel", "external");
                    LastTag.MergeAttribute("class", "last_paging");
                    LastTag.InnerHtml = "Cuối";
                    result.AppendLine(LastTag.ToString());
                }
                //result.AppendLine(" (" + pageInfo.TotalPages + " Trang)");
                return MvcHtmlString.Create(result.ToString());

            }
            else
                return MvcHtmlString.Create("");
        }

        public static MvcHtmlString PageLinksNotFirstLast(this HtmlHelper html, PageInfo pageInfo, Func<int, string> pageUrl)
        {
            if (pageInfo.TotalPages > 0)
            {
                StringBuilder result = new StringBuilder();
                for (int i = 1; i <= pageInfo.TotalPages; i++)
                {
                    TagBuilder tag = new TagBuilder("a");
                    tag.MergeAttribute("href", pageUrl(i));
                    tag.MergeAttribute("rel", "external");
                    tag.InnerHtml = i.ToString();
                    tag.AddCssClass("number_paging");
                    if (i == pageInfo.CurrentPage)
                        tag.AddCssClass("current_page");
                    result.AppendLine(tag.ToString());
                }
                return MvcHtmlString.Create(result.ToString());

            }
            else
                return MvcHtmlString.Create("");
        }

        public static MvcHtmlString PageLinksDiffCurrent(this HtmlHelper html, PageInfo pageInfo, Func<int, string> pageUrl)
        {
            if (pageInfo.TotalPages > 0)
            {
                StringBuilder result = new StringBuilder();
                if (pageInfo.TotalPages > 1 && pageInfo.CurrentPage > 1)
                {
                    //Tạo link về đầu
                    TagBuilder FirstTag = new TagBuilder("a");
                    FirstTag.MergeAttribute("href", pageUrl(1));
                    FirstTag.MergeAttribute("class", "first_paging");
                    FirstTag.InnerHtml = "Đầu";
                    result.AppendLine(FirstTag.ToString());
                }
                if (pageInfo.CurrentPage > 1)
                {
                    //Tạo link Trước nếu currentPage > 1
                    TagBuilder tag = new TagBuilder("a");
                    tag.MergeAttribute("href", pageUrl(pageInfo.CurrentPage - 1));
                    tag.MergeAttribute("class", "previous_paging");
                    tag.InnerHtml = "Sau";
                    result.AppendLine(tag.ToString());
                }
                for (int i = 1; i <= pageInfo.TotalPages; i++)
                {
                    TagBuilder tag = new TagBuilder("a");
                    tag.MergeAttribute("href", pageUrl(i));
                    tag.InnerHtml = i.ToString();
                    tag.AddCssClass("number_paging");
                    if (i == pageInfo.CurrentPage)
                        tag.AddCssClass("current");
                    result.AppendLine(tag.ToString());
                }
                if (pageInfo.CurrentPage < pageInfo.TotalPages)
                {
                    //Tạo link Sau nếu currentPage < TotalPages

                    TagBuilder tag = new TagBuilder("a");
                    tag.MergeAttribute("href", pageUrl(pageInfo.CurrentPage + 1));
                    tag.MergeAttribute("class", "next_paging");
                    tag.InnerHtml = "Tiếp";
                    result.AppendLine(tag.ToString());
                }

                if (pageInfo.TotalPages > 1 && pageInfo.CurrentPage < pageInfo.TotalPages)
                {
                    //Tạo link về cuối
                    TagBuilder LastTag = new TagBuilder("a");
                    LastTag.MergeAttribute("href", pageUrl(pageInfo.TotalPages));
                    LastTag.MergeAttribute("class", "last_paging");
                    LastTag.InnerHtml = "Cuối";
                    result.AppendLine(LastTag.ToString());
                }
                result.AppendLine(" (" + pageInfo.TotalPages + " Trang)");
                return MvcHtmlString.Create(result.ToString());

            }
            else
                return MvcHtmlString.Create("");
        }

        public static string GetNormalizeString(this HtmlHelper html,string origialString)
        {
            return origialString.Replace('&', '-').Replace('%', '\0').Replace(' ', '-').Replace('#', '\0').Replace("--", "-").Replace("--", "-");
        }

        public static MvcHtmlString PageLinksOnProductListCatePage(this HtmlHelper html, PageInfo pageInfo, Func<int, string> pageUrl)
        {
            var arr = FunctionHelpers.GenerateArrIntForPaging(pageInfo.CurrentPage, pageInfo.TotalPages, 5);
            if (pageInfo.TotalPages > 0)
            {
                var result = new StringBuilder();
                //Tạo link về đầu
                var FirstTag = new TagBuilder("a");
                FirstTag.MergeAttribute("href", pageUrl(1));
                FirstTag.InnerHtml = "Đầu trang";
                FirstTag.MergeAttribute("class", "babo-icon-category bb_pagging_First");
                result.AppendLine("<li>" + FirstTag + "</li>");
                
                for (int i = 0; i < arr.Length; i++)
                {
                    var tag = new TagBuilder("a");
                    tag.MergeAttribute("href", pageUrl(arr[i]));
                    tag.InnerHtml = arr[i].ToString();
                    tag.AddCssClass("bb_pagging_item");
                    if (arr[i] == pageInfo.CurrentPage)
                        tag.AddCssClass("bb_pagging_ac");
                    result.AppendLine("<li>" + tag + "</li>");
                }
                
                
                //Tạo link về cuối
                TagBuilder LastTag = new TagBuilder("a");
                LastTag.MergeAttribute("href", pageUrl(pageInfo.TotalPages));
                LastTag.MergeAttribute("class", "last_paging");
                LastTag.InnerHtml = "Cuối trang";
                LastTag.AddCssClass("babo-icon-category bb_pagging_Last");
                result.AppendLine("<li>" + LastTag + "</li>");
                
                return MvcHtmlString.Create("<ul class='ul'>" +result + "</ul>");
            }
            return MvcHtmlString.Create("");
        }
              
        /// <summary>
        /// Xac dinh dia chi Url duy nhat tren website
        /// </summary>
        /// <param name="html"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static MvcHtmlString CanonicalUrl(this HtmlHelper html, string path)
        {
            if (String.IsNullOrWhiteSpace(path))
            {
                var rawUrl = html.ViewContext.RequestContext.HttpContext.Request.Url;
                path = String.Format("{0}://{1}{2}", rawUrl.Scheme, rawUrl.Host, rawUrl.AbsolutePath);
            }

            path = path.ToLower();

            if (path.Count(c => c == '/') > 3)
            {
                path = path.TrimEnd('/');
            }

            if (path.EndsWith("/index"))
            {
                path = path.Substring(0, path.Length - 6);
            }

            var canonical = new TagBuilder("link");
            canonical.MergeAttribute("rel", "canonical");
            canonical.MergeAttribute("href", path);
            return new MvcHtmlString(canonical.ToString(TagRenderMode.SelfClosing));
        }

        public static MvcHtmlString CanonicalUrl(this HtmlHelper html)
        {
            var rawUrl = html.ViewContext.RequestContext.HttpContext.Request.Url;

            return CanonicalUrl(html, String.Format("{0}://{1}{2}", rawUrl.Scheme, rawUrl.Host, rawUrl.AbsolutePath));
        }
     
    }
}
